import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import javax.imageio.ImageIO;

//chu y tool nay chi dung cho font co 1 mau chu ma 1 mau nen
public class AutoMapFont {

	/**
	 * @param args
	 */
	static String pathDirectory = null;
	static String[] allImageFiles = null;
	static AutoMapFont autoMapFont;
	static String NOTEString = "----------------------------------------------------\n"
			+ "CHUONG TRINH CONVERT FONT DATA TU FILE ANH \n"
			+ "HIEN TAI CHI SU LI CHO FILE ANH PNG TU CHUONG TRINH \n"
			+ "TAT CA CAC FILE PNG TRONG THU MUC SE DUOC SU LI \n"
			+ "----------------------------------------------------";
	public static int currnetFileImageIndex = 0;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// Image img = new
		// get current dir
		System.out.println(NOTEString);
		autoMapFont = new AutoMapFont();
		pathDirectory = getCurentDirectory();
		allImageFiles = getAllListFile(pathDirectory, ".png");
		for (currnetFileImageIndex = 0; currnetFileImageIndex < allImageFiles.length; currnetFileImageIndex++)
			createSprFile(pathDirectory + "\\"
					+ allImageFiles[currnetFileImageIndex]);

	}

	public static String getCurentDirectory() {
		String str = null;
		try {
			str = new java.io.File(".").getCanonicalPath();
			System.out.println("Current directory : " + str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}

	public static String[] getAllListFile(String path, String fileExtendtion) {
		File folder = new File(path);
		String fille[] = null;
		File[] listOfFiles = folder.listFiles();

		int count = 0;
		for (int i = 0; i < listOfFiles.length; i++) {
			if (listOfFiles[i].isFile()) {
				if (listOfFiles[i].getName().indexOf(fileExtendtion) > 0)
					count++;

			}
		}
		fille = new String[count];
		count = 0;

		for (int i = 0; i < listOfFiles.length; i++) {
			if (listOfFiles[i].isFile()) {
				if (listOfFiles[i].getName().indexOf(fileExtendtion) > 0) {
					fille[count] = listOfFiles[i].getName();
					count++;
				}
			}
		}
		return fille;
	}

	public static synchronized int[] createSprFile(String filename) {
		System.out.println("CONVETTING Image Name :" + filename);
		String outputFile = filename.substring(0, filename.length() - 4)
				+ ".spr";
		BufferedImage image = null;
		InputStream is = null;
		try {
			is = new BufferedInputStream(new FileInputStream(filename));
			try {
				image = ImageIO.read(is);
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int colorBackGround = -1;

		int colorText = -1;
		colorBackGround = image.getRGB(0, 0);// ngay pixel dau tien thuong la
		// background
		int[] argb = null;
		int temp = 0;
		int count = 0;
		int[][] char_X_WIDTH = new int[1000][2];
		int charTextPosBeginY = 0;// using for all character
		int charTextPosEndY = 0;

		int numberOfAllChar = 0;
		boolean isbackground = true;// luc dau luon la back ground
		for (int i = 0; i < image.getWidth(); i++) {
			temp = image.getRGB(i, 0);
			if (temp != -1 && temp != colorBackGround)
				colorText = temp;
			if (isbackground) {
				if (temp == colorText) {
					isbackground = false;
					count = 0;
				}
			} else {// dang doc text
				if (temp == colorText) {
					count++;
				} else {
					count++;
					char_X_WIDTH[numberOfAllChar][0] = i - count;
					char_X_WIDTH[numberOfAllChar][1] = count;
					numberOfAllChar++;
					isbackground = true;
					count = 0;
				}
			}
		}
		// calulator charTextHight
		System.out.println("image.getHeight" + image.getHeight());
		System.out.println("image.getwidth" + image.getWidth());

		for (int i = 2; i < image.getHeight(); i++) {
			for (int j = 0; j < image.getWidth(); j++) {

				temp = image.getRGB(j, i);
				//System.out.print(temp + " ");
				if (temp != colorBackGround) {
					if (charTextPosBeginY == 0)
						charTextPosBeginY = i;
					if (charTextPosEndY < i) {
						charTextPosEndY = i;
					}

				}

			}
	
		}
		System.out.println("charTextPosY : " + charTextPosBeginY);
		System.out.println("charTextEndY : " + charTextPosEndY);
		// ghi ra file
		String strLineAllChar = null;
		int offset_charIndex = 0;
		try {

			BufferedReader in = null;
			try {
				in = new BufferedReader(new InputStreamReader(
						new FileInputStream("allchar.txt"), "UTF8"));
				strLineAllChar = in.readLine();
				System.out.println("File text : " + strLineAllChar);
				System.out.println("Reading Process Completly Successfully.");
			} catch (Exception e) {
				// TODO: handle exception
			}
			// Print the content on the console
			in.close();
			// strLineAllChar
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		try {
			// Create file
			// filename
			System.err.println("EXPORE FILE SAVE TO :: " + outputFile);
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(outputFile), "UTF8"));
			out.write("IMAGE font/" + allImageFiles[currnetFileImageIndex]
					+ "\n");
			out.write("COMMOND_WIDTH 8\n");
			out.write("COMMOND_HEIGHT " + (charTextPosEndY - charTextPosBeginY) +"\n");
			out.write("NUM_OF_CHAR " + numberOfAllChar + "\n");
			String str = "";

			for (int i = 0; i < numberOfAllChar; i++) {
				str = "CHAR " + strLineAllChar.charAt(i + 1) + " "
						+ char_X_WIDTH[i][0] + " " + charTextPosBeginY + " "
						+ char_X_WIDTH[i][1] + "\n";
				// System.out.print(" " + i + " : " + str);
				out.write(str);
			}
			// fstream.close();
			out.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
		return null;
	}

}
